import React, { Component } from 'react'
import {Table,Switch,Spin} from 'antd'
import {skillsSelect,skillsByIdUpdateBool} from '../../api/request/skill';
import {messageDIY} from '../../tool/message';

 class skill extends Component {
     constructor(){
         super()
         this.state={
             list:[],
             page:1,
             pageSize:5,
             total:16
         }
         let data = {
            page:1,
            pageSize:10,
            is_true:0
        }
        this.getData(data)
     }
    onChangePage=(page, pageSize)=>{
        this.setState({
            page:1,
            pageSize:pageSize,
        })
        let data = {
            page:page,
            pageSize:pageSize,
            is_true:0
        }
        this.getData(data)
        
    }
    componentDidMount() {
    
    }
    getData = (data)=>{
        this.setState({
            loading:true
          })
          skillsSelect({page:data.page,pageSize:data.pageSize,is_true:data.is_true})
        .then(res=>{
            if (res.code===200) {
                this.setState({
                    loading:false
                  })
            const data =   res.data.list.map(res=>{
                   return {
                    name: res.name,
                    key:res.id,
                    max_num: res.max_num ,
                    item_num: res.item_num ,
                    is_true:  res.is_true,
                   }
               })
                this.setState({
                    list:data,
                    page:res.data.page,
                    pageSize:res.data.pageSize,
                    total:res.data.total
                })
            }
        })
    }
    onShowSizeChange = (current, pageSize) =>{
        this.setState({
            page:1,
            pageSize:pageSize,
        })
        let data = {
            page:1,
            pageSize:pageSize,
            is_true:0
        }
        this.getData(data)
    }
    render() {
        const { list, page,  pageSize, total,loading} = this.state
        const columns = [
            {
                title:'技术名',
                dataIndex:'name',
                key: 'name',
            },
            {
                title:'最大个数',
                dataIndex:'max_num',
                key: 'max_num',
              
            },
            {
                title:'实际个数',
                dataIndex:'item_num',
                key: 'item_num',
            },
            {
                title:'是否启用',
                dataIndex:'is_true',
                key: 'is_true',
                render:(text, record, index)=>{
                    text = JSON.parse(text)
                    let bol = text ===1 ? true:false
                    return    <Switch
                    onClick={(checked,event)=>{
                        console.log(checked, record.key)
                        skillsByIdUpdateBool({is_true:checked,id: record.key})
                        .then(res=>{
                            messageDIY(res)
                            let data = {
                                page:1,
                                pageSize:this.state.pageSize,
                                is_true:0
                            }
                            this.getData(data)
                        })
                    }}
                    key={record.title+1}  checkedChildren="开" unCheckedChildren="关" checked={bol} />
                 }
            }
        ]
        return (
            <div>
               <Spin spinning={loading}>
                <Table dataSource={list} columns={columns} pagination={{
                    pageSize:pageSize,
                    current:page,
                    onChange:this.onChangePage,
                    total:total,
                    showQuickJumper:true ,
                    showSizeChanger:true,
                    onShowSizeChange:this.onShowSizeChange,
                    pageSizeOptions:['5','10', '20', '30', '40']
                }} />;
                </Spin>
            </div>
        )
    }
}
export default skill